﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace MiniIOC.IOCLIbs
{
    [DebuggerDisplay("Lifetime = {Lifetime}, ServiceType = {ServiceType}, ImplementationType = {ImplementationType}")]
    public class ServiceDescriptor
    {
        public ServiceLifetime Lifetime { get; set; }

        public Type? ServiceType { get; set; }

        public Type? ImplementationType { get; set; }

        public object? ImplementationInstance { get; set; }
    }

    public class ServiceDescriptorEqualityComparer : IEqualityComparer<ServiceDescriptor>
    {
        public bool Equals(ServiceDescriptor? x, ServiceDescriptor? y)
        {
            if(x?.ServiceType == null) return false;
            if(y?.ServiceType == null) return false;

            return object.Equals(x?.ServiceType, y?.ServiceType);
        }

        public int GetHashCode([DisallowNull] ServiceDescriptor obj)
        {
            if (obj?.ServiceType == null) return default;

            return obj.ServiceType.GetHashCode();
        }
    }
}

// https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ServiceDescriptor.cs
